package com.example.permission.service.impl;

import com.example.permission.dao.BaseMapper;
import com.example.permission.dao.PermissionMapper;
import com.example.permission.dao.RoleMapper;
import com.example.permission.dao.UserMapper;
import com.example.permission.entity.Permission;
import com.example.permission.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 用户服务层实体类
 */
@Service("userDetailsService")
public class UserServiceImpl extends BaseServiceImpl<com.example.permission.entity.User> implements UserService {

    /* 当前用户所有权限集合*/
    private Set<String> per;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private PermissionMapper permissionMapper;

    @Override           //s就是你页面输入的用户名
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        //通过s查找用户
        com.example.permission.entity.User userName = userMapper.getUserName(s);
        if(userName == null) {
            throw new UsernameNotFoundException("用户不存在");
        }
        Set<String> set = new HashSet<>();
        List<Integer> roleIds  = userMapper.getRoleIdsByUserId(userName.getId());
        for (Integer roleId : roleIds) {
            List<Integer> permissionIds = roleMapper.getPermissionIdsByRoleId(roleId);
            for (Integer permissionId : permissionIds) {
                Permission permission = permissionMapper.getIdAll(permissionId);
                set.add(permission.getPermissionName());
            }
        }
        setPer(set);
        //通过commaSeparatedStringToAuthorityList就会给你返回一个GrantedAuthority类型的list集合
        List<GrantedAuthority> list = AuthorityUtils.commaSeparatedStringToAuthorityList("admin");
        //加密之后new BCryptPasswordEncoder().encode("123")
        return new User(userName.getUsername(),new BCryptPasswordEncoder().encode(userName.getPassword()),list);
    }

    @Override
    protected BaseMapper<com.example.permission.entity.User> baseMapper() {
        return userMapper;
    }

    /**
     * 通过用户id 查找该用户的角色id
     * @param userId
     * @return
     */
    @Override
    public List<Integer> getRoleIdsByUserId(Integer userId) {
        return userMapper.getRoleIdsByUserId(userId);
    }

    @Override
    public Integer addRoleIdByUserId(Integer userId, Integer roleId) {
        return userMapper.addRoleIdByUserId(userId,roleId);
    }

    @Override
    public Integer removeRoleIdByUserId(Integer id, Integer roleId) {
        return userMapper.removeRoleIdByUserId(id,roleId);
    }

    @Override
    public Set<String> getPermission() {
        return per;
    }

    @Override
    public Integer deleteAll() {
        return userMapper.deleteAll();
    }

    public void setPer(Set<String> per){
        this.per = per;
    }
}
